package com.woniu.new_orange.personnel_model_wu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.new_orange.customization_model_zhang.vo.BulletTrainVo;
import com.woniu.new_orange.entity.Car;
import com.woniu.new_orange.entity.OrderForm;
import com.woniu.new_orange.personnel_model_wu.Dto.OrderDto;
import com.woniu.new_orange.personnel_model_wu.VO.OrderVo;
import com.woniu.new_orange.personnel_model_wu.dto.OrderDtoC;
import com.woniu.new_orange.personnel_model_wu.service.OrderFormService;
import com.woniu.new_orange.util.ObjectExcelView;
import com.woniu.new_orange.util.PageData;
import com.woniu.new_orange.util.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: new_orange->OrderFormController
 * @Description: 订单表
 * @Author: 13488
 * @Date: 2023-02-03 14:22
 * @Version： 1.0
 **/
@RestController
@RequestMapping("orders")
@Api(tags = "订单")
public class OrderFormController {

    @Resource
    private OrderFormService orderFormService;
    @ApiOperation(value = "订单条件查询",notes ="可以根据用户姓名，注册时间等查询" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderFormOrigin",value = "出发地",required = false,paramType = "query"),
            @ApiImplicitParam(name = "orderFormDate",value = "启程时间范围",required = false,paramType = "query"),
            @ApiImplicitParam(name = "currentPage",value = "当前页",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示的条数",required = true,paramType = "query",dataType = "Integer"),
    })
    @ApiResponses({
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权访问"),
            @ApiResponse(code = 404,message = "未找到")
    })
    @PostMapping("/getAll")
    public Result getAll(@RequestBody OrderDto order) {
        PageHelper.startPage(order.getCurrentPage(), order.getPageSize(), true);
        List<OrderVo> Order = orderFormService.selectAll(order);
        PageInfo<OrderVo> orderPageInfo = new PageInfo<>(Order);
        return Result.ok().data("orderList", Order).data("total", orderPageInfo.getTotal());
    }

@ApiResponses({
        @ApiResponse(code = 401,message = "未认证"),
        @ApiResponse(code = 403,message = "无权访问"),
        @ApiResponse(code = 404,message = "未找到")
})
@ApiOperation(value = "导出excel表",notes ="可以根据用户姓名，注册时间等导出" )
    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody  OrderDto order) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("订单编号"); // 1
            titles.add("出发点"); // 2
            titles.add("出行人数"); // 3
            titles.add("旅行预算"); // 4
            titles.add("计划天数"); //
            titles.add("启程时间"); //
            titles.add("备注信息"); //
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<OrderVo> orderDtoList = orderFormService.selectAll(order);
            List<PageData> varList = new ArrayList<>();
            for (OrderVo orderVo : orderDtoList) {
                PageData vpd = new PageData();
                vpd.put("var1",orderVo.getOrderFormNo());
                vpd.put("var2",orderVo.getOrderFormOrigin());
                vpd.put("var3",orderVo.getOrderFormDestination());
                vpd.put("var4",orderVo.getOrderFormBudget());
                vpd.put("var4",orderVo.getOrderFormDate());
                vpd.put("var4",orderVo.getOrderFormDesc());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
}

//zhang 2.0 12:07
    @PutMapping("/submit")
    public Result add(@RequestBody OrderForm orderForm){
        int insert = orderFormService.insertSelective(orderForm);
        return Result.ok();
    }


    @PutMapping("/addTo")
    public Result load(@RequestBody OrderForm in){
        in.setOrderFormState(1);
        int i = orderFormService.updateByorderFormNo(in);
        return Result.ok();
    }

    @GetMapping("{orderFormState}")
    public Result All(@PathVariable String orderFormState){
        int i = Integer.parseInt(orderFormState);
        List<OrderVo> orderVos = orderFormService.selectAllForC(i);
        return Result.ok().data("orderData",orderVos);
    }

    @GetMapping("/l/{customizationStaffId}")
    public Result A(@PathVariable String customizationStaffId){
        int i = Integer.parseInt(customizationStaffId);
        List<OrderVo> orderVos = orderFormService.selectByCustomizationStaffId(i);
        return Result.ok().data("orderData",orderVos);
    }
}
